<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <%@ include file="../header.jsp" %>
  <title>数字转大写-小云工作室</title>
  <script type="text/javascript">
    function toCapital(type) {
      var messageSpan = $("#message");
      messageSpan.empty();
      messageSpan.removeClass('alert-warning alert-success alert-danger');
      $("#text").val('');
      var number = $("#number").val();
      var regex1 = new RegExp("^\\d+([.]\\d{1,2})?$");
      var regex2 = new RegExp("^\\d+[.]\\d{3,}$");
      if (regex1.test(number)) {
        var url = type == 'chinese' ? 'numberToChinese' : 'numberToEnglish';
        $.get("../v1/common/" + url, {number: number, format: 'json'}, function (data) {
          $("#text").val(data.rows[0].value);
        });
      } else if (regex2.test(number)) {
        messageSpan.append("只支持最多两位小数!");
        messageSpan.addClass("alert-warning");
      } else {
        messageSpan.append("请输入数字!");
        messageSpan.addClass("alert-danger");
      }
    }
  </script>
</head>
<body>
<div class="container">
  <div class="row">
    <div class="col-lg-9">
      <h1 class="text-center">数字转大写</h1>
    </div>
  </div>
  <div class="row">
    <div class="col-lg-9">
      <form role="form">
        <div class="form-group">
          <label for="number">数字:</label>
          <input type="number" class="form-control" id="number" maxlength="8" placeholder="请输入数字"/>
        </div>
        <div class="form-group">
          <button type="button" class="btn btn-default" onclick="toCapital('chinese')">数字转中文</button>
          <button type="button" class="btn btn-default" onclick="toCapital('english')">数字转英文</button>
        </div>
        <div class="form-group">
          <label for="text">转换结果:</label>
          <textarea class="form-control" id="text"></textarea>
        </div>
        <div class="form-group">
          <p id="message" class="alert"></p>
        </div>
      </form>
    </div>
  </div>
</div>
</body>
</html>
